<?php

/**
 * Paging for database output.
 * @author dll_hell_2
 */
class paging {
    /**
     *
     * @var string Name of the page the paging is part of.
     */
    //private $page_name;//nahrad se $_SERVER
    /**
     *
     * @var int Number of page currently viewed.
     */
    private $offset;
    public static $perPage = 5;
    /**
     *
     * @var string Final html code.
     */
    private $result;
    /**
     *
     * @var int Total number of pages in list.
     */
    private $pages_count;
    private function setGET($param, $new_value)
    {
        $result = '?';
        $firstFuse = true;//na zacatek ?
        $used = false;//param jeste nebyl nahrazen
        foreach($_GET as $key => $value)
        {
            if($firstFuse == false)                
                $result .= '&';
            if($key == $param)
            {
                $result .= $key.'='.$new_value;
                $used = true;
            }else
            {
                $result .= $key.'='.$value;
            }
            $firstFuse = false;
        }
        if($used == false)
        {
            if($firstFuse == false)                
                $result .= '&';
            $result .= $param.'='.$new_value;
        }
        return $result;
    }
    /**
     *Generates html code for paging
     * @param int $start Number of the first page in output.
     */
    private function generatePaging($start)
    {
        for($i=0;($i<5) &&(($start+$i) <= $this->pages_count);++$i)
            {
                if(($start + $i) == $this->offset)
                {
                    $this->result .=$start + $i;//asi jinou barvou a fontem
                    $this->result .= " ";
                }else
                {
                    
                    $temp = sprintf("<a href=\"%s%s\">%s</a>",
                            $_SERVER['PHP_SELF'],
                            $this->setGET('page', $start + $i),
                            ($start + $i));
                    $this->result .= $temp;
                }
            }
    }
    /**
     *
     * @param string $current_page
     * @param int $total_count
     * @param int $offset
     * @param int $per_page
     */
    public function __construct(  $total_count, $offset, $per_page)
    {       
        $this->offset = $offset;
        
        $this->result = "";
        $this->pages_count = ceil($total_count / $per_page);
    }
    /**
     *Prints paging.
     * @return string Final html code.
     */
    public function __toString() {
        //vejde se to na jednu stranku
        if($this->pages_count == 1){
            return "";
        }        
        //jinak muze byt <prev tlacitko
        if($this->offset > 1)
        {            
            $this->result = sprintf("<a href=\"%s%s\">&lt;prev </a>",
                    $_SERVER['PHP_SELF'],
                    $this->setGET('page', $this->offset-1));
        }
        //maximalne pet odkazu na okolni strany
        $start = $this->offset - 2;
        if($this->offset <3)
        {
            $start = 1;
        }  
        $this->result .= $this->generatePaging($start);
        //muze byt tlacitko next>
        if($this->offset < $this->pages_count)
        {            
            $this->result .= sprintf("<a href=\"%s%s\">next&gt;</a>",
                    $_SERVER['PHP_SELF'],
                    $this->setGET('page', $this->offset + 1));
        }
        return $this->result;
    }

}
?>
